package com.edu.dgut.controller;

import com.edu.dgut.entity.goodsEntity;
import com.edu.dgut.dao.goodsRepository;
import com.edu.dgut.service.QiniuCloudUtil;
import com.edu.dgut.service.mapObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/goods/")
public class goodsController {

    @Autowired
    private goodsRepository goods;

    private mapObjectService mapObject = new mapObjectService();

    //查询所有商品
    @RequestMapping("getAllgoods")
    @ResponseBody
    public Map<String, Object> findAll(){
        return mapObject.put(goods.findAll());
    }

    //通过商品名查找商品
    @RequestMapping("getByGoods_name")
    @ResponseBody
    public Map<String, Object> getByGoods_name(String goods_name) {
        List<goodsEntity> result = goods.findByGoodsNameLike("%"+goods_name+"%");
        return mapObject.put(result);
    }

    //通过商品id查找商品
    @RequestMapping("getByGoods_id")
    @ResponseBody
    public Map<String, Object> getByGoods_name(int goods_id) {
        List<goodsEntity> result = goods.findByGoodsId(goods_id);
        return mapObject.put(result);
    }

    //添加或修改商品信息
    @RequestMapping("modifyGoods")
    @ResponseBody
    public Map<String, Object> saveOrModifyGoods(@RequestBody goodsEntity new_goods) {
        //如果输入的商品名对应信息不存在，则插入新信息，否则修改商品名对应的各类信息
        List<goodsEntity> old_goods = goods.findByGoodsNameLike(new_goods.getGoodsName());
        if(old_goods.size() != 0){
            new_goods.setGoodsId(old_goods.get(0).getGoodsId());
            if(new_goods.getImg() == ""){
                new_goods.setImg(old_goods.get(0).getImg());
            }
        }
        new_goods = goods.save(new_goods);
        Map<String,Object> map = new HashMap<String,Object>();
        if(new_goods != null) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //删除商品信息
    @RequestMapping("deleteByGoods_id")
    @ResponseBody
    public Map<String, Object> deleteByGoods_id(int goods_id) {
        int i = goods.deleteByGoodsId(goods_id);
        Map<String,Object> map = new HashMap<String,Object>();
        if(i >= 1) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //上传图片，返回图片url
    @ResponseBody
    @RequestMapping(value="/uploadImg")
    public Map<String,Object> uploadImg(@RequestParam("file") MultipartFile image) {
        Map<String,Object> map = new HashMap<>();
        if (image.isEmpty()) {
            map.put("status",0);
            map.put("msg","文件为空，请重新上传");
            return map;
        }

        try {
            byte[] bytes = image.getBytes();
            String imageName = UUID.randomUUID().toString();

            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                map.put("status",1);
                map.put("msg","文件上传成功");
                map.put("url",url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        } catch (IOException e) {
            map.put("status",0);
            map.put("msg","文件上传发生异常");
            return map;
        }
    }
}
